/**
 * @description 基线项目入口文件
 */
import React, {Component} from 'react';
import ReactDOM from 'react-dom';
import {Router, Route, IndexRoute, hashHistory, IndexRedirect} from 'react-router';
import {QMFetch, QMConst} from 'qm-ux';
import QMRoute from 'QMRoute'
QMFetch.register(401, () => window.location.href = QMConst.HOST["v_sso_server"] + "/logout");
import GaUtil from "util/ga-util";

import AccountBind from "react-router-proxy?name=weixinOpen!./bind";
import MainContent from "react-router-proxy?name=weixinOpen!./main";
import Overview from "react-router-proxy?name=weixinOpen!./overview";
import News from "react-router-proxy?name=weixinOpen!./news";
import AutoReply from "react-router-proxy?name=weixinOpen!./autoreply";
import Setting from "react-router-proxy?name=weixinOpen!./setting";
import Message from "react-router-proxy?name=weixinOpen!./message";
import Mass from "react-router-proxy?name=weixinOpen!./mass";
import Menu from "react-router-proxy?name=weixinOpen!./menu";

import '../style/index.less';

/*所有事件集合*/
window.eventMap = new Map();

/*添加事件代理*/
window.EventTarget.prototype.addEventListenerProxy = window.EventTarget.prototype.addEventListener;
window.EventTarget.prototype.addEventListener = function (type, event, capture) {
    this.addEventListenerProxy(type, event, capture);
    let listener = {type, event, capture};
    if (window.eventMap.has(this)) {
        let events = window.eventMap.get(this);
        events.push(listener);
        window.eventMap.set(this, events);
    } else {
        let events = [listener];
        events.target = this;
        window.eventMap.set(this, events);
    }
    return this;
}

/*移除事件代理(支持按类型批量移除)*/
window.EventTarget.prototype.removeEventListenerProxy = window.EventTarget.prototype.removeEventListener;
window.EventTarget.prototype.removeEventListener = function (type, event, capture) {
    if (event instanceof Function) {
        this.removeEventListenerProxy(type, event, capture);
    } else {
        let events = window.eventMap.get(this);
        events.filter(o => o.type == type).forEach(o => {
            this.removeEventListenerProxy(type, o.event, capture || event);
            events.splice(events.indexOf(o), 1);
        });
    }
    return this;
}


const recordGA = (data) => {
    const navList = {
        "/overview": "微信概况",
        "/message": "消息管理",
        "/mass": "群发消息",
        "/autoreply": "自动回复",
        "/news": "图文素材",
        "/news/news": "新建图文",
        "/news/adnews": "新建高级图文",
        "/menu": "自定义菜单",
        "/setting": "公众号设置",
    };

    const pathname = data.location.pathname;

    GaUtil.addEvent({eventAction: "菜单导航", eventLabel: navList[pathname] || pathname});
};

/**
 * @author
 */
ReactDOM.render((
    <QMRoute history={hashHistory}>
        <Route>
            <Route component={MainContent}>
                <IndexRedirect to="/overview"/>
                <Route path="/overview" onEnter={recordGA} components={Overview}/>
                <Route path="/message" onEnter={recordGA} components={Message}/>
                <Route path="/mass" onEnter={recordGA} components={Mass}/>
                <Route path="/news(/:page)" onEnter={recordGA} components={News}/>
                <Route path="/autoreply" onEnter={recordGA} components={AutoReply}/>
                <Route path="/menu" onEnter={recordGA} components={Menu}/>
                <Route path="/setting" onEnter={recordGA} components={Setting}/>
            </Route>
            <Route path="/bind" components={AccountBind}/>
        </Route>
    </QMRoute>
), document.getElementById("react-content"));
